import collections


class Solution(object):
    def smallestFactorization(self, num):
        """
        :type num: int
        :rtype: int
        """
        if num == 1:
            return 1
        yinzi = collections.defaultdict(int)

        while True:
            if num in {2, 3, 5, 7}:
                yinzi[num] += 1
                break
            else:
                flag = False
                for i in [2, 3, 5, 7]:
                    if num % i == 0:
                        yinzi[i] += 1
                        num = num // i
                        flag = True
                        break
                if not flag:
                    return 0
        print(yinzi)
        out = []
        if yinzi[3] % 2 == 1:
            if yinzi[2] % 3 >= 1:
                out.extend([2] * (yinzi[2] % 3 - 1))
                out.append(6)
            else:
                out.append(3)
        else:
            if yinzi[2] % 3 == 2:
                out.append(4)
            else:
                out.extend([2] * (yinzi[2] % 3))
        out.extend([5] * (yinzi[5]))
        out.extend([7] * (yinzi[7]))
        out.extend([9] * (yinzi[3] // 2))
        out.extend([8] * (yinzi[2] // 3))
        out.sort()
        n = len(out)
        total = 0
        for i in range(len(out)):
            total += (out[i] * 10 ** (n - i - 1))
        if total <= 2 ** 31 - 1:
            return total
        else:
            return 0


data = Solution()
num = 30
print(data.smallestFactorization(num))
